package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.runningriding.component.cache.MyRedisTemplate;
import com.quanyan.runningriding.constans.PublicConstans;
import com.quanyan.runningriding.entity.db.*;
import com.quanyan.runningriding.entity.vo.run.VideoVo;
import com.quanyan.runningriding.entity.vo.run.req.RunVideoFirstType;
import com.quanyan.runningriding.entity.vo.run.req.RunVideoSecondType;
import com.quanyan.runningriding.entity.vo.run.req.VideoSimple;
import com.quanyan.runningriding.entity.vo.run.resp.*;
import com.quanyan.runningriding.mapper.*;
import com.quanyan.runningriding.service.RunVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * RunVideoServiceImpl
 *
 * @author Tu Chun
 * @date 2016/9/20
 */
@Service
public class RunVideoServiceImpl implements RunVideoService {
    @Autowired
    private TbRunVideoLibraryMapper tbRunVideoLibraryMapper;

    @Autowired
    private TbRunVideoTypeMapper tbRunVideoTypeMapper;

    @Autowired
    private TbRunVideoSecondTypeMapper tbRunVideoSecondTypeMapper;

    @Autowired
    private RunVideoMapper runVideoMapper;

    @Autowired
    private TbRunUserVideoRecordMapper tbRunUserVideoRecordMapper;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public APIResponse<?> batchUploadVideo(List<VideoVo> videoVos) {

        int reuslt = runVideoMapper.batchUploadVideo(videoVos);
        if (reuslt > 0) {
            for (int i = 0; i < videoVos.size(); i++) {
                VideoVo videoVo = videoVos.get(i);
                Byte type = videoVo.getType();
                //获取视频总时间、数量
                TbRunVideoTypeExample tbRunVideoTypeExample = new TbRunVideoTypeExample();
                tbRunVideoTypeExample.createCriteria().andTypeEqualTo(type);
                List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(tbRunVideoTypeExample);
                TbRunVideoType tbRunVideoTypeOld = null;
                if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0) {
                    tbRunVideoTypeOld = tbRunVideoTypes.get(0);
                }
                TbRunVideoType tbRunVideoType = new TbRunVideoType();
                tbRunVideoType.setTime(tbRunVideoTypeOld.getTime() + videoVo.getTime());
                tbRunVideoType.setNumber(tbRunVideoTypeOld.getNumber() + 1);
                tbRunVideoTypeMapper.updateByExampleSelective(tbRunVideoType, tbRunVideoTypeExample);
            }

            return APIResponse.returnSuccess("批量上传视频成功");
        } else {
            return APIResponse.returnSuccess("批量上传视频失败");
        }

    }

    @Override
    public PageObj<List<VideoVo>> videoList(String name, Byte firstType, String firstTypeName, Byte secondType, String secondTypeName, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pageStart", (pageNum - 1) * pageSize);
        paramMap.put("pageSize", pageSize);
        paramMap.put("name", name);
        paramMap.put("firstType", firstType);
        paramMap.put("firstTypeName", firstTypeName);
        paramMap.put("secondType", secondType);
        paramMap.put("secondTypeName", secondTypeName);
        TbRunVideoLibraryExample tbRunVideoTypeExample = new TbRunVideoLibraryExample();
        TbRunVideoLibraryExample.Criteria criteria = tbRunVideoTypeExample.createCriteria();
        if (!StringUtil.isBlankOrNull(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        Integer count = runVideoMapper.videoCount(paramMap);

        Page page = new Page((pageNum - 1) * pageSize, pageSize);
        tbRunVideoTypeExample.setPage(page);
        List<VideoVo> videoVos = runVideoMapper.getVideoList(paramMap);

        PageObj pageObj = PageObj.create(count, pageNum, pageSize, videoVos);
        return pageObj;
    }

    @Override
    public APIResponse<?> updateVideo(VideoVo VideoVo) {
        TbRunVideoLibrary tbRunVideoLibrary = new TbRunVideoLibrary();
        tbRunVideoLibrary.setId(VideoVo.getId());
        tbRunVideoLibrary.setName(VideoVo.getName());
        tbRunVideoLibrary.setTime(VideoVo.getTime());
        tbRunVideoLibrary.setActionFeel(VideoVo.getActionFeel());
        tbRunVideoLibrary.setMainPoints(VideoVo.getMainPoints());
        tbRunVideoLibrary.setThumbnailUrl(VideoVo.getThumbnailUrl());
        tbRunVideoLibrary.setVideoUrl(VideoVo.getVideoUrl());
        tbRunVideoLibrary.setType(VideoVo.getType());
        tbRunVideoLibrary.setSecondType(VideoVo.getSecondType());
        tbRunVideoLibrary.setNote(VideoVo.getNote());
        tbRunVideoLibrary.setSize(VideoVo.getSize());
        tbRunVideoLibrary.setSmallUrl(VideoVo.getSmallUrl());
        Integer i = tbRunVideoLibraryMapper.updateByPrimaryKeySelective(tbRunVideoLibrary);
        if (i > 0)
            return APIResponse.returnSuccess("跟新成功");
        return APIResponse.returnFail("跟新异常");
    }

    @Override
    public APIResponse<?> deleteVideo(Integer videoId) {
        if (videoId == 0 || videoId == null) {
            APIResponse.returnFail("参数异常");
        }
        TbRunVideoLibrary tbRunVideoLibrary = tbRunVideoLibraryMapper.selectByPrimaryKey(videoId);
        Integer i = tbRunVideoLibraryMapper.deleteByPrimaryKey(videoId);
        if (i > 0) {
            TbRunVideoTypeExample tbRunVideoTypeExample = new TbRunVideoTypeExample();
            tbRunVideoTypeExample.createCriteria().andTypeEqualTo(tbRunVideoLibrary.getType());
            List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(tbRunVideoTypeExample);
            TbRunVideoType tbRunVideoTypeOld = null;
            if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0) {
                tbRunVideoTypeOld = tbRunVideoTypes.get(0);
            }
            TbRunVideoType tbRunVideoType = new TbRunVideoType();
            tbRunVideoType.setTime(tbRunVideoTypeOld.getTime() - tbRunVideoLibrary.getTime());
            tbRunVideoType.setNumber(tbRunVideoTypeOld.getNumber() - 1);
            tbRunVideoTypeMapper.updateByExampleSelective(tbRunVideoType, tbRunVideoTypeExample);
            return APIResponse.returnSuccess("删除成功");
        }
        return APIResponse.returnFail("删除异常");
    }

    @Override
    public APIResponse<List<VideoType>> getVideoTypeList() {
        List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(null);
        List<VideoType> videoTypes = transVideoType(tbRunVideoTypes);
        return APIResponse.returnSuccess(videoTypes);
    }

    @Override
    public PageObj<VideoTypeDetail> getVideoTypeDetail(Byte type, Integer pageNum, Integer pageSize) {
        //得到某类型视频
        TbRunVideoTypeExample tbRunVideoTypeExample = new TbRunVideoTypeExample();
        tbRunVideoTypeExample.createCriteria().andTypeEqualTo(type);
        List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(tbRunVideoTypeExample);
        List<VideoType> videoTypes = transVideoType(tbRunVideoTypes);
        //获取某类型下所有视频
        TbRunVideoLibraryExample tbRunVideoLibraryExample = new TbRunVideoLibraryExample();
        tbRunVideoLibraryExample.createCriteria().andTypeEqualTo(type);
        int count = tbRunVideoLibraryMapper.countByExample(tbRunVideoLibraryExample);
        Page page = new Page((pageNum - 1) * pageSize, pageSize);
        tbRunVideoLibraryExample.setPage(page);
        List<TbRunVideoLibrary> tbRunVideoLibraries = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
        List<VideoVo> videoVos = transVideoVo(tbRunVideoLibraries);
        VideoTypeDetail videoTypeDetail = new VideoTypeDetail();
        if (videoTypes != null && videoTypes.size() > 0)
            videoTypeDetail.setVideoType(videoTypes.get(0));
        videoTypeDetail.setVideoVos(videoVos);

        return PageObj.create(count, pageNum, pageSize, videoTypeDetail);
    }


    @Override
    public APIResponse<?> editFirstType(RunVideoFirstType firstType) {
        TbRunVideoTypeExample tbRunVideoTypeExample = new TbRunVideoTypeExample();
        tbRunVideoTypeExample.createCriteria().andTypeEqualTo(firstType.getType());
        List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(tbRunVideoTypeExample);
        Byte operationType = firstType.getOperationType();
        if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0 && operationType == 2) {
            TbRunVideoType tbRunVideoType = new TbRunVideoType();
            tbRunVideoType.setName(firstType.getName());
            tbRunVideoType.setVideoDescribe(firstType.getVideoDescribe());
            tbRunVideoType.setSuitableCrowd(firstType.getSuitableCrowd());
            tbRunVideoType.setImgUrl(firstType.getFirstTypeImg());
            int i = tbRunVideoTypeMapper.updateByExampleSelective(tbRunVideoType, tbRunVideoTypeExample);
            if (i > 0)
                return APIResponse.returnSuccess("修改成功");
            else
                return APIResponse.returnFail("修改失败");
        } else if (operationType == 1) {//如果不存在则添加
            TbRunVideoType tbRunVideoType = new TbRunVideoType();
            tbRunVideoType.setType(firstType.getType());
            tbRunVideoType.setName(firstType.getName());
            tbRunVideoType.setVideoDescribe(firstType.getVideoDescribe());
            tbRunVideoType.setSuitableCrowd(firstType.getSuitableCrowd());
            tbRunVideoType.setCreateTime(new Date());
            tbRunVideoType.setUpdateTime(new Date());
            tbRunVideoType.setNumber(0);
            tbRunVideoType.setTime(0l);
            tbRunVideoType.setImgUrl(firstType.getFirstTypeImg());
            int i = tbRunVideoTypeMapper.insertSelective(tbRunVideoType);
            if (i > 0)
                return APIResponse.returnSuccess("添加成功");
            else
                return APIResponse.returnFail("添加失败");
        }
        return APIResponse.returnFail("服务器异常");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public APIResponse<?> deleteFirstType(Byte firstType) {
        //删除一级类型下所有视频
        TbRunVideoLibraryExample tbRunVideoLibraryExample = new TbRunVideoLibraryExample();
        tbRunVideoLibraryExample.createCriteria().andTypeEqualTo(firstType);
        tbRunVideoLibraryMapper.deleteByExample(tbRunVideoLibraryExample);
        //删除一级类型下对应的二级类型
        TbRunVideoSecondTypeExample tbRunVideoSecondTypeExample = new TbRunVideoSecondTypeExample();
        tbRunVideoSecondTypeExample.createCriteria().andFirstTypeEqualTo(firstType);
        tbRunVideoSecondTypeMapper.deleteByExample(tbRunVideoSecondTypeExample);
        //删除一级类型
        TbRunVideoTypeExample tbRunVideoTypeExample = new TbRunVideoTypeExample();
        tbRunVideoTypeExample.createCriteria().andTypeEqualTo(firstType);
        int i2 = tbRunVideoTypeMapper.deleteByExample(tbRunVideoTypeExample);
        if (i2 > 0)
            return APIResponse.returnSuccess("删除成功");
        else
            return APIResponse.returnFail("删除失败");
    }


    @Override
    public APIResponse<?> editSecondType(RunVideoSecondType secondType) {
        TbRunVideoSecondTypeExample tbRunVideoSecondTypeExample = new TbRunVideoSecondTypeExample();
        tbRunVideoSecondTypeExample.createCriteria().andFirstTypeEqualTo(secondType.getFirstType())
                .andSecondTypeEqualTo(secondType.getSecondType());
        List<TbRunVideoSecondType> tbRunVideoSecondTypes = tbRunVideoSecondTypeMapper.selectByExample(tbRunVideoSecondTypeExample);
        Byte operationType = secondType.getOperationType();
        //如果不为空则更新
        if (tbRunVideoSecondTypes != null && tbRunVideoSecondTypes.size() > 0 && operationType == 2) {
            TbRunVideoSecondType tbRunVideoSecondType = new TbRunVideoSecondType();
            tbRunVideoSecondType.setName(secondType.getName());
            tbRunVideoSecondType.setTypeDescribe(secondType.getTypeDescribe());
            tbRunVideoSecondType.setRemark(secondType.getRemark());
            int i = tbRunVideoSecondTypeMapper.updateByExampleSelective(tbRunVideoSecondType, tbRunVideoSecondTypeExample);
            if (i > 0)
                return APIResponse.returnSuccess("修改成功");
            else
                return APIResponse.returnFail("修改失败");
        } else if (operationType == 1) {//如果为空则添加
            TbRunVideoSecondType tbRunVideoSecondType = new TbRunVideoSecondType();
            tbRunVideoSecondType.setFirstType(secondType.getFirstType());
            tbRunVideoSecondType.setSecondType(secondType.getSecondType());
            tbRunVideoSecondType.setName(secondType.getName());
            tbRunVideoSecondType.setTypeDescribe(secondType.getTypeDescribe());
            tbRunVideoSecondType.setRemark(secondType.getRemark());
            tbRunVideoSecondType.setCreateTime(new Date());
            tbRunVideoSecondType.setUpdateTime(new Date());
            int i = tbRunVideoSecondTypeMapper.insertSelective(tbRunVideoSecondType);
            if (i > 0)
                return APIResponse.returnSuccess("添加成功");
            else
                return APIResponse.returnFail("添加失败");
        }
        return APIResponse.returnFail("服务器异常");

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public APIResponse<?> deleteSecondType(Byte firstType, Byte secondType) {
        //删除二级类型下视频
        TbRunVideoLibraryExample tbRunVideoLibraryExample = new TbRunVideoLibraryExample();
        tbRunVideoLibraryExample.createCriteria().andTypeEqualTo(firstType)
                .andSecondTypeEqualTo(secondType);
        tbRunVideoLibraryMapper.deleteByExample(tbRunVideoLibraryExample);
        //删除二级类型
        TbRunVideoSecondTypeExample tbRunVideoSecondTypeExample = new TbRunVideoSecondTypeExample();
        tbRunVideoSecondTypeExample.createCriteria().andFirstTypeEqualTo(firstType)
                .andSecondTypeEqualTo(secondType);
        int i = tbRunVideoSecondTypeMapper.deleteByExample(tbRunVideoSecondTypeExample);
        if (i > 0)
            return APIResponse.returnSuccess("删除成功");
        else
            return APIResponse.returnFail("删除失败");
    }

    @Override
    public APIResponse<List<VideoTypeCollection>> showTypeList() {
        List<VideoTypeCollection> videoTypeCollections = new ArrayList<VideoTypeCollection>();
        List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(null);
        if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0) {
            for (int i = 0; i < tbRunVideoTypes.size(); i++) {
                VideoTypeCollection videoTypeCollection = new VideoTypeCollection();
                TbRunVideoType tbRunVideoType = tbRunVideoTypes.get(i);
                TbRunVideoSecondTypeExample tbRunVideoSecondTypeExample = new TbRunVideoSecondTypeExample();
                tbRunVideoSecondTypeExample.createCriteria().andFirstTypeEqualTo(tbRunVideoType.getType());
                List<TbRunVideoSecondType> tbRunVideoSecondTypes = tbRunVideoSecondTypeMapper.selectByExample(tbRunVideoSecondTypeExample);
                videoTypeCollection.setFirstType(tbRunVideoType);
                videoTypeCollection.setSecondTypes(tbRunVideoSecondTypes);
                videoTypeCollections.add(videoTypeCollection);
            }
        }
        return APIResponse.returnSuccess(videoTypeCollections);
    }

    @Override
    public APIResponse<List<VideoFirstTypeSimple>> getFirstTypeList() {
        List<VideoFirstTypeSimple> videoFirstTypeSimples = new ArrayList<VideoFirstTypeSimple>();

        List<TbRunVideoType> tbRunVideoTypes = tbRunVideoTypeMapper.selectByExample(null);
        if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0) {
            for (int i = 0; i < tbRunVideoTypes.size(); i++) {
                TbRunVideoType tbRunVideoType = tbRunVideoTypes.get(i);
                VideoFirstTypeSimple videoFirstTypeSimple = new VideoFirstTypeSimple();
                videoFirstTypeSimple.setName(tbRunVideoType.getName());
                videoFirstTypeSimple.setType(tbRunVideoType.getType());
                videoFirstTypeSimples.add(videoFirstTypeSimple);
            }
        }
        return APIResponse.returnSuccess(videoFirstTypeSimples);
    }

    @Override
    public APIResponse<List<VideoSecondTypeSimple>> getSecondTypeList(Byte firstType) {
        List<VideoSecondTypeSimple> videoSecondTypeSimples = new ArrayList<VideoSecondTypeSimple>();
        TbRunVideoSecondTypeExample tbRunVideoSecondTypeExample = new TbRunVideoSecondTypeExample();
        tbRunVideoSecondTypeExample.createCriteria().andFirstTypeEqualTo(firstType);
        List<TbRunVideoSecondType> tbRunVideoSecondTypes = tbRunVideoSecondTypeMapper.selectByExample(tbRunVideoSecondTypeExample);
        if (tbRunVideoSecondTypes != null && tbRunVideoSecondTypes.size() > 0) {
            for (int i = 0; i < tbRunVideoSecondTypes.size(); i++) {
                TbRunVideoSecondType tbRunVideoSecondType = tbRunVideoSecondTypes.get(i);
                VideoSecondTypeSimple videoSecondTypeSimple = new VideoSecondTypeSimple();
                videoSecondTypeSimple.setType(tbRunVideoSecondType.getSecondType());
                videoSecondTypeSimple.setName(tbRunVideoSecondType.getName());
                videoSecondTypeSimples.add(videoSecondTypeSimple);
            }
        }
        return APIResponse.returnSuccess(videoSecondTypeSimples);
    }

    @Override
    public APIResponse<?> userVideoRecord(String userId, Integer videoId) {
        TbRunUserVideoRecordExample tbRunUserVideoRecordExample = new TbRunUserVideoRecordExample();
        tbRunUserVideoRecordExample.createCriteria().andUserIdEqualTo(userId)
                .andVideoIdEqualTo(videoId);
        List<TbRunUserVideoRecord> tbRunUserVideoRecords = tbRunUserVideoRecordMapper.selectByExample(tbRunUserVideoRecordExample);

        //已经记录了用户视频记录则更新
        if (tbRunUserVideoRecords != null && tbRunUserVideoRecords.size() > 0) {
            TbRunUserVideoRecord tbRunUserVideoRecord = new TbRunUserVideoRecord();
            tbRunUserVideoRecord.setUpdateTime(new Date());
            tbRunUserVideoRecordMapper.updateByExampleSelective(tbRunUserVideoRecord, tbRunUserVideoRecordExample);
        } else {//记录用户视频记录
            TbRunUserVideoRecord tbRunUserVideoRecord = new TbRunUserVideoRecord();
            tbRunUserVideoRecord.setUserId(userId);
            tbRunUserVideoRecord.setVideoId(videoId);
            tbRunUserVideoRecord.setCreateTime(new Date());
            tbRunUserVideoRecord.setUpdateTime(new Date());
            tbRunUserVideoRecordMapper.insertSelective(tbRunUserVideoRecord);
        }
        return APIResponse.returnSuccess("记录用户视频记录成功");
    }

    @Override
    public APIResponse<List<VideoVo>> videoRecommend(String userId, Double distance, Long time, Long avgPace) {
        List<VideoVo> videoVos = new ArrayList<VideoVo>();
        RedisKey key = new RedisKey(RedisConstants.RUN_RIDING_CACHE_PREFIX, PublicConstans.RUN_USER_ID + userId);
        Long min = time / 60;
        avgPace=avgPace/60;
        //List<Integer> videoIds = (List<Integer>) myRedisTemplate.get(key);
        List<Integer> videoIds = new ArrayList<Integer>();
        List<Integer> videoIdsNew = new ArrayList<Integer>();
        if (distance > PublicConstans.KILO8MILEAGE || min > PublicConstans.MIN60TIME) {
            //视频1 - 基础拉伸类（大腿）
            TbRunVideoLibraryExample tbRunVideoLibraryExample = new TbRunVideoLibraryExample();
            TbRunVideoLibraryExample.Criteria criteria = tbRunVideoLibraryExample.createCriteria();

            criteria.andTypeEqualTo(PublicConstans.BASIC_STRETCHING).andSecondTypeEqualTo(PublicConstans.THIGH);
            if (videoIds != null && videoIds.size() > 0) {
                criteria.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo1 = null;
            if (tbRunVideoLibrarys != null && tbRunVideoLibrarys.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys.size());
                videoVo1 = transVideoVo(tbRunVideoLibrarys.get(random));
                //防止重复
                videoIds.add(videoVo1.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo1.getId());
            }
            videoVos.add(videoVo1);

            //视频2 - 基础拉伸类（小腿）
            tbRunVideoLibraryExample.clear();
            TbRunVideoLibraryExample.Criteria criteria2 = tbRunVideoLibraryExample.createCriteria();
            criteria2.andTypeEqualTo(PublicConstans.BASIC_STRETCHING).andSecondTypeEqualTo(PublicConstans.LOWER_LEG);
            if (videoIds != null && videoIds.size() > 0) {
                criteria2.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys2 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo2 = null;
            if (tbRunVideoLibrarys2 != null && tbRunVideoLibrarys2.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys2.size());
                videoVo2 = transVideoVo(tbRunVideoLibrarys2.get(random));
                //防止重复
                videoIds.add(videoVo2.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo2.getId());
            }
            videoVos.add(videoVo2);


            //视频3 - 全面拉伸类
            tbRunVideoLibraryExample.clear();
            TbRunVideoLibraryExample.Criteria criteria3 = tbRunVideoLibraryExample.createCriteria();
            criteria3.andTypeEqualTo(PublicConstans.FULL_STRETCH);
            if (videoIds != null && videoIds.size() > 0) {
                criteria3.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys3 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo3 = null;
            if (tbRunVideoLibrarys3 != null && tbRunVideoLibrarys3.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys3.size());
                videoVo3 = transVideoVo(tbRunVideoLibrarys3.get(random));
                //防止重复
                videoIds.add(videoVo3.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo3.getId());
            }

            videoVos.add(videoVo3);

            //视频4 - 全面拉伸类
            tbRunVideoLibraryExample.clear();
            TbRunVideoLibraryExample.Criteria criteria4 = tbRunVideoLibraryExample.createCriteria();
            criteria4.andTypeEqualTo(PublicConstans.FULL_STRETCH);
            if (videoIds != null && videoIds.size() > 0) {
                criteria4.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys4 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo4 = null;
            if (tbRunVideoLibrarys4 != null && tbRunVideoLibrarys4.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys4.size());
                videoVo4 = transVideoVo(tbRunVideoLibrarys4.get(random));
                //防止重复
                videoIds.add(videoVo4.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo4.getId());
            }
            videoVos.add(videoVo4);

            //视频5 - 动态调整
            if(distance>5){
                //视频5推送全面拉伸类
                tbRunVideoLibraryExample.clear();
                TbRunVideoLibraryExample.Criteria criteria5 = tbRunVideoLibraryExample.createCriteria();
                criteria5.andTypeEqualTo(PublicConstans.FULL_STRETCH);
                if (videoIds != null && videoIds.size() > 0) {
                    criteria5.andIdNotIn(videoIds);
                }
                List<TbRunVideoLibrary> tbRunVideoLibrarys5 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
                VideoVo videoVo5 = null;
                if (tbRunVideoLibrarys5 != null && tbRunVideoLibrarys5.size() > 0) {
                    int random = (int) (Math.random() * tbRunVideoLibrarys5.size());
                    videoVo5 = transVideoVo(tbRunVideoLibrarys5.get(random));
                    //记录最新的videoId
                    videoIdsNew.add(videoVo5.getId());
                }
                videoVos.add(videoVo5);
            }else {
                if (avgPace > PublicConstans.MIN7TIME) {
                    //视频5推送跑力锻炼类
                    tbRunVideoLibraryExample.clear();
                    TbRunVideoLibraryExample.Criteria criteria5 = tbRunVideoLibraryExample.createCriteria();
                    criteria5.andTypeEqualTo(PublicConstans.RUNNING_EXERCISE);
                    if (videoIds != null && videoIds.size() > 0) {
                        criteria5.andIdNotIn(videoIds);
                    }
                    List<TbRunVideoLibrary> tbRunVideoLibrarys5 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
                    VideoVo videoVo5 = null;
                    if (tbRunVideoLibrarys5 != null && tbRunVideoLibrarys5.size() > 0) {
                        int random = (int) (Math.random() * tbRunVideoLibrarys5.size());
                        videoVo5 = transVideoVo(tbRunVideoLibrarys5.get(random));
                        //记录最新的videoId
                        videoIdsNew.add(videoVo5.getId());
                    }
                    videoVos.add(videoVo5);

                }else{
                    //视频5推送全面拉伸类
                    tbRunVideoLibraryExample.clear();
                    TbRunVideoLibraryExample.Criteria criteria5 = tbRunVideoLibraryExample.createCriteria();
                    criteria5.andTypeEqualTo(PublicConstans.FULL_STRETCH);
                    if (videoIds != null && videoIds.size() > 0) {
                        criteria5.andIdNotIn(videoIds);
                    }
                    List<TbRunVideoLibrary> tbRunVideoLibrarys5 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
                    VideoVo videoVo5 = null;
                    if (tbRunVideoLibrarys5 != null && tbRunVideoLibrarys5.size() > 0) {
                        int random = (int) (Math.random() * tbRunVideoLibrarys5.size());
                        videoVo5 = transVideoVo(tbRunVideoLibrarys5.get(random));
                        //记录最新的videoId
                        videoIdsNew.add(videoVo5.getId());
                    }
                    videoVos.add(videoVo5);
                }
            }
        } else {
            //视频1 - 基础拉伸类（大腿）
            TbRunVideoLibraryExample tbRunVideoLibraryExample = new TbRunVideoLibraryExample();
            TbRunVideoLibraryExample.Criteria criteria = tbRunVideoLibraryExample.createCriteria();

            criteria.andTypeEqualTo(PublicConstans.BASIC_STRETCHING).andSecondTypeEqualTo(PublicConstans.THIGH);
            if (videoIds != null && videoIds.size() > 0) {
                criteria.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo1 = null;
            if (tbRunVideoLibrarys != null && tbRunVideoLibrarys.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys.size());
                videoVo1 = transVideoVo(tbRunVideoLibrarys.get(random));
                //防止重复
                videoIds.add(videoVo1.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo1.getId());
            }
            videoVos.add(videoVo1);

            //视频2 - 基础拉伸类（小腿）
            tbRunVideoLibraryExample.clear();
            TbRunVideoLibraryExample.Criteria criteria2 = tbRunVideoLibraryExample.createCriteria();
            criteria2.andTypeEqualTo(PublicConstans.BASIC_STRETCHING).andSecondTypeEqualTo(PublicConstans.LOWER_LEG);
            if (videoIds != null && videoIds.size() > 0) {
                criteria2.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys2 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo2 = null;
            if (tbRunVideoLibrarys2 != null && tbRunVideoLibrarys2.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys2.size());
                videoVo2 = transVideoVo(tbRunVideoLibrarys2.get(random));
                //防止重复
                videoIds.add(videoVo2.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo2.getId());
            }
            videoVos.add(videoVo2);

            int week = DateUtils.getDayOfWeek(new Date());
            int exercise1 = 0;
            int exercise2 = 0;
            int exercise3 = 0;
            int exercise4 = 0;
            if (distance <= PublicConstans.KILO4MILEAGE) {
                //跑力锻炼视频权重值“1”
                exercise4++;
            } else {
                //增加全面拉伸类权重“1"
                exercise2++;
            }

            if (min <= PublicConstans.MIN30TIME) {
                if (avgPace > PublicConstans.MIN7TIME) {
                    //增加亚健康拉伸类权重"1"
                    exercise3++;
                } else {
                    //增加全面拉伸类权重"1"
                    exercise2++;
                }
            } else {
                if (avgPace > PublicConstans.MIN7TIME) {
                    //增加推送跑力锻炼类权重"1"
                    exercise4++;
                } else {
                    //增加全面拉伸类权重"1"
                    exercise2++;
                }
            }
            List<Integer> exercises = new ArrayList<Integer>();
            exercises.add(exercise1);
            exercises.add(exercise2);
            exercises.add(exercise3);
            exercises.add(exercise4);
            //视频3
            Integer flag = getRecommendVideo(exercises);
            tbRunVideoLibraryExample.clear();
            TbRunVideoLibraryExample.Criteria criteria3 = tbRunVideoLibraryExample.createCriteria();
            criteria3.andTypeEqualTo(flag.byteValue());
            if (videoIds != null && videoIds.size() > 0) {
                criteria3.andIdNotIn(videoIds);
            }
            List<TbRunVideoLibrary> tbRunVideoLibrarys3 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
            VideoVo videoVo3 = null;
            if (tbRunVideoLibrarys3 != null && tbRunVideoLibrarys3.size() > 0) {
                int random = (int) (Math.random() * tbRunVideoLibrarys3.size());
                videoVo3 = transVideoVo(tbRunVideoLibrarys3.get(random));
                //防止重复
                videoIds.add(videoVo3.getId());
                //记录最新的videoId
                videoIdsNew.add(videoVo3.getId());
            }

            videoVos.add(videoVo3);

            if (week == 6 || week == 7) {//周末时间多推送二条视频
                //视频4 - 全面拉伸类（小腿）
                tbRunVideoLibraryExample.clear();
                TbRunVideoLibraryExample.Criteria criteria4 = tbRunVideoLibraryExample.createCriteria();
                criteria4.andTypeEqualTo(PublicConstans.FULL_STRETCH).andSecondTypeEqualTo(PublicConstans.LOWER_LEG);
                if (videoIds != null && videoIds.size() > 0) {
                    criteria4.andIdNotIn(videoIds);
                }
                List<TbRunVideoLibrary> tbRunVideoLibrarys4 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
                VideoVo videoVo4 = null;
                if (tbRunVideoLibrarys4 != null && tbRunVideoLibrarys4.size() > 0) {
                    int random = (int) (Math.random() * tbRunVideoLibrarys4.size());
                    videoVo4 = transVideoVo(tbRunVideoLibrarys4.get(random));
                    //防止重复
                    videoIds.add(videoVo4.getId());
                    //记录最新的videoId
                    videoIdsNew.add(videoVo4.getId());
                }
                videoVos.add(videoVo4);

                //视频5
                tbRunVideoLibraryExample.clear();
                TbRunVideoLibraryExample.Criteria criteria5 = tbRunVideoLibraryExample.createCriteria();
                criteria5.andTypeEqualTo(PublicConstans.FULL_STRETCH);
                if (videoIds != null && videoIds.size() > 0) {
                    criteria5.andIdNotIn(videoIds);
                }
                List<TbRunVideoLibrary> tbRunVideoLibrarys5 = tbRunVideoLibraryMapper.selectByExample(tbRunVideoLibraryExample);
                VideoVo videoVo5 = null;
                if (tbRunVideoLibrarys5 != null && tbRunVideoLibrarys5.size() > 0) {
                    int random = (int) (Math.random() * tbRunVideoLibrarys5.size());
                    videoVo5 = transVideoVo(tbRunVideoLibrarys5.get(random));
                    //记录最新的videoId
                    videoIdsNew.add(videoVo5.getId());
                }
                videoVos.add(videoVo5);

            }
        }
        //更新redis
        //myRedisTemplate.set(key,videoIdsNew);
        return APIResponse.returnSuccess(videoVos);
    }

    private Integer getRecommendVideo(List<Integer> exercises) {
        VideoSimple videoSimple = new VideoSimple();
        Integer exercise = Collections.max(exercises);
        int flag = 0;
        for (int i = 0; i < exercises.size(); i++) {
            if (exercise == exercises.get(i)) {
                flag = i + 1;
            }
        }

        return flag;
    }

    private List<VideoVo> transVideoVo(List<TbRunVideoLibrary> tbRunVideoLibraries) {
        List<VideoVo> videoVos = new ArrayList<VideoVo>();
        if (tbRunVideoLibraries != null && tbRunVideoLibraries.size() > 0) {
            for (int i = 0; i < tbRunVideoLibraries.size(); i++) {
                VideoVo videoVo = new VideoVo();
                TbRunVideoLibrary runVideoLibrary = tbRunVideoLibraries.get(i);
                videoVo.setTime(runVideoLibrary.getTime());
                videoVo.setNote(runVideoLibrary.getNote());
                videoVo.setType(runVideoLibrary.getType());
                videoVo.setSecondType(runVideoLibrary.getSecondType());
                videoVo.setActionFeel(runVideoLibrary.getActionFeel());
                videoVo.setId(runVideoLibrary.getId());
                videoVo.setActionFeel(runVideoLibrary.getActionFeel());
                videoVo.setMainPoints(runVideoLibrary.getMainPoints());
                videoVo.setThumbnailUrl(runVideoLibrary.getThumbnailUrl());
                videoVo.setSmallUrl(runVideoLibrary.getSmallUrl());
                videoVo.setVideoUrl(runVideoLibrary.getVideoUrl());
                videoVo.setName(runVideoLibrary.getName());
                videoVo.setSize(runVideoLibrary.getSize());
                videoVos.add(videoVo);
            }
        }

        return videoVos;
    }

    private VideoVo transVideoVo(TbRunVideoLibrary runVideoLibrary) {
        VideoVo videoVo = new VideoVo();
        if (runVideoLibrary != null) {
            videoVo.setTime(runVideoLibrary.getTime());
            videoVo.setNote(runVideoLibrary.getNote());
            videoVo.setType(runVideoLibrary.getType());
            videoVo.setSecondType(runVideoLibrary.getSecondType());
            videoVo.setActionFeel(runVideoLibrary.getActionFeel());
            videoVo.setId(runVideoLibrary.getId());
            videoVo.setActionFeel(runVideoLibrary.getActionFeel());
            videoVo.setMainPoints(runVideoLibrary.getMainPoints());
            videoVo.setThumbnailUrl(runVideoLibrary.getThumbnailUrl());
            videoVo.setVideoUrl(runVideoLibrary.getVideoUrl());
            videoVo.setName(runVideoLibrary.getName());
            videoVo.setSize(runVideoLibrary.getSize());
        }
        return videoVo;
    }

    private List<VideoType> transVideoType(List<TbRunVideoType> tbRunVideoTypes) {
        List<VideoType> videoTypes = new ArrayList<VideoType>();
        if (tbRunVideoTypes != null && tbRunVideoTypes.size() > 0) {
            for (int i = 0; i < tbRunVideoTypes.size(); i++) {
                VideoType videoType = new VideoType();
                TbRunVideoType runVideoType = tbRunVideoTypes.get(i);
                videoType.setName(runVideoType.getName());
                videoType.setDescribe(runVideoType.getVideoDescribe());
                videoType.setNumber(runVideoType.getNumber());
                videoType.setSuitableCrowd(runVideoType.getSuitableCrowd());
                videoType.setTime(runVideoType.getTime());
                videoType.setType(runVideoType.getType());
                videoType.setImgUrl(runVideoType.getImgUrl());
                videoTypes.add(videoType);
            }
        }

        return videoTypes;
    }
}
